package com.hl.sfzy.controller;

import com.hl.sfzy.constant.CommonConstant;
import com.hl.sfzy.constant.Result;
import com.hl.sfzy.entity.SysAnnouncement;
import com.hl.sfzy.service.SysAnnouncementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author ffc
 * @version 1.0
 * @description: TODO
 * @className: SysAnnouncementController
 * @date 2024/7/11 20:12
 */

@RestController
public class SysAnnouncementController {

    @Autowired
    private SysAnnouncementService sysAnnouncementService;

    /*新增用户*/
    @PostMapping("/sysAnnouncement")
    public Result add(@RequestBody SysAnnouncement sysAnnouncement){
        int num = sysAnnouncementService.add(sysAnnouncement);
        return num>0? Result.success():Result.error();
    }
   //根据id删除
    @DeleteMapping("/sysAnnouncement/{id}")
    public Result remove(@PathVariable("id") Integer id){
        int num = sysAnnouncementService.remove(id);
        return num>0? Result.success():Result.error();
    }

    //批量删除
    @DeleteMapping("/sysAnnouncement")
    public Result removeBatch(Integer[] ids){
        int num = sysAnnouncementService.removeBatch(ids);
        return num>0? Result.success():Result.error();
    }

    //更新
    @PutMapping("/sysAnnouncement")
    public Result edit(@RequestBody SysAnnouncement sysAnnouncement){
        int num = sysAnnouncementService.edit(sysAnnouncement);
        return num>0? Result.success():Result.error();
    }

    //根据id查询
    @GetMapping("/sysAnnouncement/{id}")
    public Result findById(@PathVariable("id") Integer id){
        SysAnnouncement byId = sysAnnouncementService.findById(id);
        return Result.success(byId);
    }

    //根据条件查询不分页，也可以查所有
    @GetMapping("/sysAnnouncement")
    public Result find(SysAnnouncement sysAnnouncement){
        List<SysAnnouncement> list = sysAnnouncementService.find(sysAnnouncement);
        return Result.success(list);
    }

    //分页和条件查询
    @GetMapping("/sysAnnouncement/page")
    public Result findPage(
            @RequestParam(value = CommonConstant.PAGE_NUM,required = false,defaultValue = CommonConstant.PAGE_NUM_DEFAULT) Integer pageNum,
            @RequestParam(value = CommonConstant.PAGE_SIZE,required = false,defaultValue = CommonConstant.PAGE_SIZE_DEFAULT) Integer pageSize,
            SysAnnouncement sysAnnouncement){
        Map<String ,Object> map = sysAnnouncementService.findPage(pageNum,pageSize,sysAnnouncement);
        return Result.success(map);
    }
}
